package com.tianyu.inspection.controller;

import com.alibaba.fastjson2.JSONObject;
import com.tianyu.inspection.common.Constants;
import com.tianyu.inspection.common.annotation.Permission;
import com.tianyu.inspection.common.enums.RoState;
import com.tianyu.inspection.common.enums.Role;
import com.tianyu.inspection.entity.BasicInformation;
import com.tianyu.inspection.entity.SysTypeDef;
import com.tianyu.inspection.entity.model.OperateMo;
import com.tianyu.inspection.entity.model.QueryMo;
import com.tianyu.inspection.entity.vo.Ro;
import com.tianyu.inspection.entity.vo.ServiceRo;
import com.tianyu.inspection.service.api.ISysTypeDefService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**********************************
 * @Author YSW
 * @Description 通用设施接口
 * @Date 2021/1/15 - 18:40
 **********************************/

@RestController
@RequestMapping("/api/2.0/sysType")
public class SysTypeDefController {

    @Resource
    private ISysTypeDefService sysTypeDefService;

    @GetMapping(value = "/mapLayer/list")
    @Permission(value = 4, tags = Constants.API_PATROL)
    public Ro<Object> getMapLayerTypeList(@RequestHeader("u-projectId") Integer projectId) {
        return new Ro<>().setContent(sysTypeDefService.getMapLayerTypeList(projectId));
    }

    @GetMapping(value = "/mapLayer/tree")
    @Permission(value = 4, tags = Constants.API_PATROL)
    public Ro<Object> getMapLayerTypeTree(@RequestHeader("u-projectId") Integer projectId) {
        return new Ro<>().setContent(sysTypeDefService.getMapLayerTypeTree(projectId, 83));
    }

    @GetMapping(value = "/list/global")
    @Permission(value = 2, tags = Constants.API_ADMIN)
    public Ro<Object> getGlobalList(HttpServletRequest request, @RequestHeader("u-projectId") Integer projectId) {
        int roleId = (int) request.getAttribute("roleId");
        Ro<Object> ro = new Ro<>();
        if (roleId == Role.ROLE_SUPER_ADMIN.getValue()) {
            QueryMo queryMo = QueryMo.getQueryMo(request);
            queryMo.build(SysTypeDef.class, null);
            return ro.setContent(sysTypeDefService.searchListPage(queryMo));
        } else {
            return ro.setContent(sysTypeDefService.searchList(projectId));
        }
    }

    @GetMapping(value = "/list")
    @Permission(value = 4, tags = Constants.API_PATROL)
    public Ro<Object> getList(HttpServletRequest request, @RequestHeader("u-projectId") Integer projectId) {
        int roleId = (int) request.getAttribute("roleId");
        Ro<Object> ro = new Ro<>();
        if (roleId == Role.ROLE_SUPER_ADMIN.getValue()) {
            QueryMo queryMo = QueryMo.getQueryMo(request);
            queryMo.build(SysTypeDef.class, null);
            return ro.setContent(sysTypeDefService.searchListPage(queryMo));
        } else {
            return ro.setContent(sysTypeDefService.searchList(projectId));
        }
    }

    @PostMapping(value = "/insert")
    @Permission(value = 1, tags = Constants.API_SUPER)
    public Ro<SysTypeDef> insert(@RequestBody JSONObject jsonObject, @RequestHeader("u-projectId") Integer projectId) {
        Ro<SysTypeDef> ro = new Ro<>();
        final SysTypeDef sysTypeDef = jsonObject.toJavaObject(SysTypeDef.class);
        sysTypeDef.setProjectId(projectId);
        if (!sysTypeDefService.insert(sysTypeDef)) {
            ro.setResultEnum(RoState.FAIL);
        }
        return ro;
    }

    @PatchMapping(value = "/modify")
    @Permission(value = 1, tags = Constants.API_SUPER)
    public Ro<SysTypeDef> modify(@RequestBody JSONObject jsonObject) {
        Ro<SysTypeDef> ro = new Ro<>();
        if (Objects.isNull(jsonObject.get("id"))) {
            return ro.setResultEnum(RoState.FAIL).setMessage("缺少[id]字段,请传参后重试");
        }
        final SysTypeDef sysTypeDef = jsonObject.toJavaObject(SysTypeDef.class);
        if (!sysTypeDefService.updateById(sysTypeDef)) {
            ro.setResultEnum(RoState.FAIL).setMessage("参数无匹配,修改失败");
        }
        return ro;
    }

    @DeleteMapping(value = "/delete/{id}")
    @Permission(value = 1, tags = Constants.API_SUPER)
    public Ro<SysTypeDef> delete(@PathVariable Integer id,@RequestHeader("u-projectId") Integer projectId) {
        Ro<SysTypeDef> ro = new Ro<>();
        if (!sysTypeDefService.deleteById(id,projectId)) {
            ro.setResultEnum(RoState.FAIL).setMessage("无匹配ID,删除失败");
        }
        return ro;
    }

}
